package com.example.restful.controller;

import com.example.restful.entity.User;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/User")
public class UserController {
    public List<User> getUsers() {
        List<User> userList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            String name = "用户";
            String email = "user";
            String phone = "1387721011";
            User user=new User();
            user.setName(name+i);
            user.setAge(18);
            user.setName(email+i+"@qq.com");
            user.setPassword("123456");
            user.setPhone(phone+i);
            userList.add(user);
        }
        return userList;
    }

    //获取所有用户信息
    @GetMapping("/list")
    public List<User> getList(){
        return getUsers();
    }

    //获取指定用户数据
    @GetMapping("/{id}")
    public User getUser(@PathVariable int id){
        return getUsers().get(id);
    }

    @PostMapping("/{add}")
    public List<User> addUser(@RequestBody User user){
        //获取用户列表--list
        //生成一行用户数据--users
        //users.add(user)
        //return users
        List<User> users =new ArrayList<>();
        users.add(user);

        return users;

    }
    @PostMapping("/update/{id}")
    public List<User> updateUser(@PathVariable Integer id,@RequestBody User user){
        //获取用户列表--list
        //查找用户数据--user.id
        //list.update(user)
        //返回数据：return list
        List<User> users=getUsers();
        users.get(id).setName(user.getName());
        users.get(id).setAge(user.getAge());
        users.get(id).setEmail(user.getEmail());
        users.get(id).setPassword(user.getPassword());
        users.get(id).setPhone(user.getPhone());

        return users;
    }
    @DeleteMapping("/id")
    public List<User> delUser(@PathVariable int id){
        //获取用户列表--list
        //查找用户数据--user.id
        //list.remove(user)
        //返回数据:return list
        List<User> users=getUsers();
        User user=users.get(id);
        users.remove(user);
        return users;
    }
}
